Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

465
Vistas
Mongoose query not running - "cursor.toArray is not a function"

MongoDB beginner, having trouble getting queries to work. Was following a tutorial of sorts and it was a demo notes app. Their syntax for saving new notes works fine.

However when it comes to printing out the list of notes, there seems to be something wrong in the syntax given to me or something im doing wrong.

const mongoose = require("mongoose");

const url =
    "mongodb+srv://Saif:<password>@cluster0.8d2lb.mongodb.net/notes-app?retryWrites=true&w=majority";

mongoose.connect(url, {
    useNewUrlParser: true,
});

const noteSchema = new mongoose.Schema({
    content: String,
    date: Date,
    important: Boolean,
});

const Note = mongoose.model("Note", noteSchema);

Note.find({}).then((result) => {
    result.forEach((note) => {
        console.log(note);
    });
    mongoose.connection.close();
});

After looking up documentation, the actual syntax of find is a little different where they pass in a callback instead of using promises. But changing that block to use a callback still doesnt work

Note.find({}, (error, data) => {
    if (error) {
        console.log(error);
        
    } else {
        data.forEach((note) => {
            console.log(note);
        })   
    }
    mongoose.connection.close()

})

Error

TypeError: cursor.toArray is not a function
    at model.Query.<anonymous> (D:\Folders\Documents\CS.........

(Use `node --trace-warnings ...` to show where the warning was created)
(node:27108) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:27108) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

.find method from model returns Query object not Cursor

For cursor you need to do .exec

Note.find({}).exec((error, data) => {
    if (error) {
        console.log(error);
        
    } else {
        data.forEach((note) => {
            console.log(note);
        })   
    }
    mongoose.connection.close()

})
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda